package com.fourtwenty.hemptress.plugin;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DependencyTree<E> {
	private Set<DependencyTree<E>> dependencies = new HashSet<>(1); //good best guess
	private String name;
	private E element;
	
	public static void main(String[] args) throws Exception {
		DependencyTree<Object> core = new DependencyTree<Object>("core", null);
		
		for(int i = 0; i < 5; i++) {
			DependencyTree<Object> dep = new DependencyTree<Object>(i+"", null);
			
			for(int j = 0; j < 3; j++) {
				dep.addDependency(new DependencyTree<Object>(i+":" + j+"", null));
			}
			
			core.addDependency(dep);
		}
		
		System.out.println(core.flatten());
	}
	
	public DependencyTree(String name, E element) {
		this.name = name;
		this.element = element;
	}
	
	public DependencyTree<E> find(E e, Comparator<E> comp) { 
		if(element != null && comp.compare(e, element) == 0) {
			return this;
		}
		
		for(DependencyTree<E> dep : dependencies) {
			DependencyTree<E> found;
			if((found = dep.find(e, comp)) != null) {
				return found;
			}
		}
		
		return null;
	}
	
	public DependencyTree<E> find(E e) {
		if(element != null && element.equals(e)) {
			return this;
		}
		
		for(DependencyTree<E> dep : dependencies) {
			DependencyTree<E> found;
			if((found = dep.find(e)) != null) {
				return found;
			}
		}
		
		return null;
	}
	
	public void addDependency(DependencyTree<E> dependency) throws CircularDependencyException {
		if(dependency.dependsOn(this)) {
			throw new CircularDependencyException(name + " added as dependency of itself: " + dependency.getDependencyList(this));
		}
		
		dependencies.add(dependency);
	}
	
	public List<DependencyTree<E>> flatten() {
		List<DependencyTree<E>> list = new ArrayList<>();
		
		for(DependencyTree<E> dep : dependencies) {
			list.addAll(dep.flatten());
			list.add(dep);
		}
		
		return list;
	}
	
	public List<DependencyTree<E>> getDependencyList(DependencyTree<E> dependency) {
		if(!dependsOn(dependency)) {
			return new ArrayList<>();
		}
		
		List<DependencyTree<E>> list = new ArrayList<>();
		
		for(DependencyTree<E> dep : dependencies) {
			if(dep.dependsOn(dependency)) {
				list.add(dep);
				list.addAll(dep.getDependencyList(dependency));
				break;
			}
		}
		
		return list;
	}
	
	public boolean dependsOn(DependencyTree<E> dependency) {
		for(DependencyTree<E> dep : dependencies) {
			if(dep.equals(dependency)) {
				return true;
			}
			
			if(dep.hasDependencies()) {
				return dep.dependsOn(dependency);
			}
		}
		
		return false;
	}
	
	public boolean hasDependencies() {
		return dependencies.size() > 0;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public E getElement() {
		return element;
	}

	public void setElement(E element) {
		this.element = element;
	}

	public Set<DependencyTree<E>> getDependencies() {
		return dependencies;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((element == null) ? 0 : element.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}



	@Override
	public String toString() {
		//return "DependencyTree [dependencies=" + dependencies + ", name=" + name + "]";
		return name;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof DependencyTree)) {
			return false;
		}
		DependencyTree<?> other = (DependencyTree<?>) obj;
		
		if (element == null) {
			if (other.element != null) {
				return false;
			}
		} else if (!element.equals(other.element)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}
}
